﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Vision.Common
{
    public class NodeCamera0
    {
        private Object obj = new object();
        /// <summary>
        /// 输入图像
        /// </summary>
        public Bitmap Image { get; set; } = null;
        /// <summary>
        /// 子区
        /// </summary>
        public List<NodeComponent> Components { get; set; } = new List<NodeComponent>();
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="image"></param>

        public void Init(Bitmap image)
        {
            lock(obj)
            {
                Image = (Bitmap)image.Clone();
            }
            foreach (var comp in Components)
                foreach(var item in comp.Elements)
                    item.IsOK = true;
        }

        public void Reset(bool bBase2Cur = true)
        {
            foreach (var comp in Components)
            {
                comp.Reset(bBase2Cur);
            }
                
        }
        /// <summary>
        /// 获取图像
        /// </summary>
        /// <returns></returns>
        public Bitmap GetImg()
        {
            Bitmap bmp=null;
            lock(obj)
            {
                if(Image !=null)
                    bmp = (Bitmap)Image.Clone();
            }
            return bmp;
        }

        public void SaveImage(string filePath,bool bSplit=false)
        {
            string OKPath = filePath + "\\OK";
            string NGPath = filePath + "\\NG";
            if (!Directory.Exists(OKPath))
                Directory.CreateDirectory(OKPath);
            if (!Directory.Exists(NGPath))
                Directory.CreateDirectory(NGPath);
            string imgPath;
            foreach (var compCur in Components)
            {
                foreach(var itemCur in compCur.Elements)
                {
                    if (bSplit)
                        imgPath = itemCur.IsOK ? OKPath : NGPath;// + "\\" + comp.Name + "-" + item.Name + ".png";
                    else
                        imgPath = filePath;
                    itemCur.Image?.Save($"{imgPath}\\{compCur.Name}-{itemCur.Name}.png", System.Drawing.Imaging.ImageFormat.Png);
                }
            }
        }
    }
}
